home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 285_02 / output.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-07-08  |  30.5 KB  |  1,372 lines

  1. /* Output the generated parsing program for bison,
  2.    Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3.  
  4. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  5. WARRANTY.  No author or distributor accepts responsibility to anyone
  6. for the consequences of using it or for whether it serves any
  7. particular purpose or works at all, unless he says so in writing.
  8. Refer to the BISON General Public License for full details.
  9.  
  10. Everyone is granted permission to copy, modify and redistribute BISON,
  11. but only under the conditions described in the BISON General Public
  12. License.  A copy of this license is supposed to have been given to you
  13. along with BISON so you can know your rights and responsibilities.  It
  14. should be in a file named COPYING.  Among other things, the copyright
  15. notice and this notice must be preserved on all copies.
  16.  
  17.  In other words, you are welcome to use, share and improve this program.
  18.  You are forbidden to forbid anyone else to use, share and improve
  19.  what you give them.   Help stamp out software-hoarding!  */
  20.  
  21. /* functions to output parsing data to various files.  Entries are:
  22.  
  23.   output_headers ()
  24.  
  25. Output constant strings to the beginning of certain files.
  26.  
  27.   output_trailers()
  28.  
  29. Output constant strings to the ends of certain files.
  30.  
  31.   output ()
  32.  
  33. Output the parsing tables and the parser code to ftable.
  34.  
  35. The parser tables consist of:  (starred ones needed only for the semantic parser)
  36.  
  37. yytranslate = vector mapping yylex's token numbers into bison's token numbers.
  38.  
  39. yytname = vector of string-names indexed by bison token number
  40.  
  41. yyrline = vector of line-numbers of all rules.  For yydebug printouts.
  42.  
  43. * yyrhs = vector of items of all rules.
  44.         This is exactly what ritems contains.
  45.  
  46. * yyprhs[r] = index in yyrhs of first item for rule r.
  47.  
  48. yyr1[r] = symbol number of symbol that rule r derives.
  49.  
  50. yyr2[r] = number of symbols composing right hand side of rule r.
  51.  
  52. * yystos[s] = the symbol number of the symbol that leads to state s.
  53.  
  54. yydefact[s] = default rule to reduce with in state s,
  55.           when yytable doesn't specify something else to do.
  56.           Zero means the default is an error.
  57.  
  58. yydefgoto[i] = default state to go to after a reduction of a rule that
  59.            generates variable ntokens + i, except when yytable
  60.            specifies something else to do.
  61.  
  62. yypact[s] = index in yytable of the portion describing state s.
  63.             The lookahed token's type is used to index that portion
  64.             to find out what to do.
  65.  
  66.         If the value in yytable is positive,
  67.         we shift the token and go to that state.
  68.  
  69.         If the value is negative, it is minus a rule number to reduce by.
  70.  
  71.         If the value is zero, the default action from yydefact[s] is used.
  72.  
  73. yypgoto[i] = the index in yytable of the portion describing 
  74.              what to do after reducing a rule that derives variable i + ntokens.
  75.              This portion is indexed by the parser state number
  76.          as of before the text for this nonterminal was read.
  77.          The value from yytable is the state to go to.
  78.  
  79. yytable = a vector filled with portions for different uses,
  80.           found via yypact and yypgoto.
  81.  
  82. yycheck = a vector indexed in parallel with yytable.
  83.       It indicates, in a roundabout way, the bounds of the
  84.       portion you are trying to examine.
  85.  
  86.       Suppose that the portion of yytable starts at index p
  87.       and the index to be examined within the portion is i.
  88.       Then if yycheck[p+i] != i, i is outside the bounds
  89.       of what is actually allocated, and the default
  90.       (from yydefact or yydefgoto) should be used.
  91.       Otherwise, yytable[p+i] should be used.
  92.  
  93. YYFINAL = the state number of the termination state.
  94. YYFLAG = most negative short int.  Used to flag ??
  95. YYNTBASE = ntokens.
  96.  
  97. */
  98.  
  99. #include <stdio.h>
  100. #include <stdlib.h>
  101. #include <ctype.h>
  102. #include <string.h>
  103.  
  104. #include "machine.h"
  105. #include "new.h"
  106. #include "files.h"
  107. #include "gram.h"
  108. #include "state.h"
  109. #include "bison.h"
  110. #include "output.h"
  111. #include "bison.h"
  112.  
  113. /* #define    MAXTABLE 32767 */
  114. /* 
  115.     Microsoft v5.1 variations:
  116.         malloc() gets 65516 bytes MAX in one object,
  117.         so MAXTABLE is constrained to 65516 / sizeof( short int))
  118.         = 32758 or less.
  119. */
  120. #define MAXTABLE 32700
  121.  
  122. #define DOSTRACK 4608           /* bytes per physical track in vanilla DOS */
  123.  
  124. extern int debugflag;
  125. extern int nolinesflag;
  126.  
  127. extern char **tags;
  128. extern int tokensetsize;
  129. extern int final_state;
  130. extern core **state_table;
  131. extern shifts **shift_table;
  132. extern errs **err_table;
  133. extern reductions **reduction_table;
  134. extern short *accessing_symbol;
  135. extern unsigned *LA;
  136. extern short *LAruleno;
  137. extern short *lookaheads;
  138. extern char *consistent;
  139. extern short *goto_map;
  140. extern short *from_state;
  141. extern short *to_state;
  142.  
  143.  
  144. static int nvectors;
  145. static int nentries;
  146. static short **froms;
  147. static short **tos;
  148. static short *tally;
  149. static short *width;
  150. static short *actrow;
  151. static short *state_count;
  152. static short *order;
  153. static short *base;
  154. static short *pos;
  155. static short *table;
  156. static short *check;
  157. static int lowzero;
  158. static int high;
  159.  
  160.  
  161. #define    GUARDSTR    "\n#include \"%s\"\nextern int yyerror;\n\
  162. extern int yycost;\nextern char * yymsg;\nextern YYSTYPE yyval;\n\n\
  163. yyguard(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  164. register YYLTYPE *yylsp;\n\
  165. {\n  yyerror = 0;\nyycost = 0;\n  yymsg = 0;\nswitch (n)\n    {"
  166.  
  167. #define    ACTSTR        "\n#include \"%s\"\nextern YYSTYPE yyval;\
  168. \nextern int yychar;\
  169. yyaction(n, yyvsp, yylsp)\nregister int n;\nregister YYSTYPE *yyvsp;\n\
  170. register YYLTYPE *yylsp;\n{\n  switch (n)\n{"
  171.  
  172. #define    ACTSTR_SIMPLE    "\n  switch (yyn) {\n"
  173.  
  174.  
  175. void output_headers()
  176. {
  177.     if (semantic_parser)
  178.         fprintf(fguard, GUARDSTR, attrsfile);
  179.     fprintf(faction, (semantic_parser ? ACTSTR : ACTSTR_SIMPLE), attrsfile);
  180.  
  181.     /*  if (semantic_parser)    JF moved this below
  182.     fprintf(ftable, "#include \"%s\"\n", attrsfile);
  183.   fprintf(ftable, "#include <stdio.h>\n\n"); */
  184. }
  185.  
  186. void output_trailers()
  187. {
  188.     if (semantic_parser)
  189.     {
  190.         fprintf(fguard, "\n    }\n}\n");
  191.         fprintf(faction, "\n    }\n}\n");
  192.     }
  193.     else
  194.         fprintf(faction, "\n}\n");
  195. }
  196.  
  197.  
  198. void output()
  199. {
  200.     int c;
  201.     /* output_token_defines(ftable);    /* JF put out token defines FIRST */
  202.  
  203.     if (!semantic_parser)        /* JF Put out other stuff */
  204.     {
  205.         rewind(fattrs);
  206.         while ((c=getc(fattrs))!=EOF)
  207.             putc(c,ftable);
  208.     }
  209.  
  210.     if (debugflag)
  211.         fprintf(ftable, "#define YYDEBUG\n");
  212.  
  213.     if (semantic_parser)
  214.         fprintf(ftable, "#include \"%s\"\n", attrsfile);
  215.     fprintf(ftable, "#include <stdio.h>\n\n");
  216.  
  217.     /* Make "const" do nothing if not in ANSI C.  */
  218.     fprintf (ftable, "#ifndef __STDC__\n#define const\n#endif\n\n");
  219.  
  220.     free_itemsets();
  221.     output_defines();
  222.     output_token_translations();
  223.     if (semantic_parser)
  224.         output_gram();
  225.     FREE(ritem);
  226.     if (semantic_parser)
  227.         output_stos();
  228.     output_rule_data();
  229.     output_actions();
  230.     output_parser();
  231.     output_program();
  232. }
  233.  
  234. void output_token_translations()
  235. {
  236.     register int i, j;
  237.     /*   register short *sp; JF unused */
  238.     if (translations)
  239.     {
  240.         fprintf(ftable,
  241.           "\n#define YYTRANSLATE(x) ((unsigned)(x) <= %d ? yytranslate[x] : %d)\n",
  242.           max_user_token_number, nsyms);
  243.  
  244.         if (ntokens < 127)  /* play it very safe; check maximum element value.  */
  245.             fprintf(ftable, "\nstatic const char yytranslate[] = {     0");
  246.         else
  247.             fprintf(ftable, "\nstatic const short yytranslate[] = {     0");
  248.  
  249.         j = 10;
  250.         for (i = 1; i <= max_user_token_number; i++)
  251.         {
  252.             putc(',', ftable);
  253.  
  254.             if (j >= 10)
  255.             {
  256.                 putc('\n', ftable);
  257.                 j = 1;
  258.             }
  259.             else
  260.             {
  261.                 j++;
  262.             }
  263.  
  264.             fprintf(ftable, "%4d", token_translations[i]);
  265.         }
  266.  
  267.         fprintf(ftable, "\n};\n");
  268.     }
  269.     else
  270.     {
  271.         fprintf(ftable, "\n#define YYTRANSLATE(x) (x)\n");
  272.     }
  273. }
  274.  
  275.  
  276.  
  277. void output_gram()
  278. {
  279.     register int i;
  280.     register int j;
  281.     register short *sp;
  282.     fprintf(ftable, "\nstatic const short yyprhs[] = {     0");
  283.  
  284.     j = 10;
  285.     for (i = 1; i <= nrules; i++)
  286.     {
  287.         putc(',', ftable);
  288.  
  289.         if (j >= 10)
  290.         {
  291.             putc('\n', ftable);
  292.             j = 1;
  293.         }
  294.         else
  295.         {
  296.             j++;
  297.         }
  298.  
  299.         fprintf(ftable, "%4d", rrhs[i]);
  300.     }
  301.  
  302.     fprintf(ftable, "\n};\n\nstatic const short yyrhs[] = {%4d", ritem[0]);
  303.  
  304.     j = 10;
  305.     for (sp = ritem + 1; *sp; sp++)
  306.     {
  307.         putc(',', ftable);
  308.  
  309.         if (j >= 10)
  310.         {
  311.             putc('\n', ftable);
  312.             j = 1;
  313.         }
  314.         else
  315.         {
  316.             j++;
  317.         }
  318.  
  319.         if (*sp > 0)
  320.             fprintf(ftable, "%4d", *sp);
  321.         else
  322.             fprintf(ftable, "     0");
  323.     }
  324.  
  325.     fprintf(ftable, "\n};\n");
  326. }
  327.  
  328.  
  329.  
  330. void output_stos()
  331. {
  332.     register int i;
  333.     register int j;
  334.     fprintf(ftable, "\nstatic const short yystos[] = {     0");
  335.  
  336.     j = 10;
  337.     for (i = 1; i < nstates; i++)
  338.     {
  339.         putc(',', ftable);
  340.  
  341.         if (j >= 10)
  342.         {
  343.             putc('\n', ftable);
  344.             j = 1;
  345.         }
  346.         else
  347.         {
  348.             j++;
  349.         }
  350.  
  351.         fprintf(ftable, "%6d", accessing_symbol[i]);
  352.     }
  353.  
  354.     fprintf(ftable, "\n};\n");
  355. }
  356.  
  357.  
  358.  
  359. void output_rule_data()
  360. {
  361.     register int i;
  362.     register int j;
  363.     fprintf(ftable, "\nstatic const short yyrline[] = {     0");
  364.  
  365.     j = 10;
  366.     for (i = 1; i <= nrules; i++)
  367.     {
  368.         putc(',', ftable);
  369.  
  370.         if (j >= 10)
  371.         {
  372.             putc('\n', ftable);
  373.             j = 1;
  374.         }
  375.         else
  376.         {
  377.             j++;
  378.         }
  379.  
  380.         fprintf(ftable, "%4d", rline[i]);
  381.     }
  382.  
  383.     /* Output the table of token names.  */
  384.  
  385.     fprintf(ftable, "\n};\n\nstatic const char * const yytname[] = {     0");
  386.  
  387.     j = 10;
  388.     for (i = 1; i <= ntokens; i++)
  389.     {
  390.         register char *p;
  391.         putc(',', ftable);
  392.  
  393.         if (j >= 10)
  394.         {
  395.             putc('\n', ftable);
  396.             j = 1;
  397.         }
  398.         else
  399.         {
  400.             j++;
  401.         }
  402.  
  403.         putc ('\"', ftable);
  404.  
  405.         for (p = tags[i]; *p; p++)
  406.             if (*p == '"' || *p == '\\')
  407.                 fprintf(ftable, "\\%c", *p);
  408.             else if (*p == '\n')
  409.                 fprintf(ftable, "\\n");
  410.             else if (*p == '\t')
  411.                 fprintf(ftable, "\\t");
  412.             else if (*p == '\b')
  413.                 fprintf(ftable, "\\b");
  414.             else if (*p < 040 || *p >= 0177)
  415.                 fprintf(ftable, "\\%03o", *p);
  416.             else
  417.                 putc(*p, ftable);
  418.  
  419.         putc ('\"', ftable);
  420.     }
  421.  
  422.     fprintf(ftable, "\n};\n\nstatic const short yyr1[] = {     0");
  423.  
  424.     j = 10;
  425.     for (i = 1; i <= nrules; i++)
  426.     {
  427.         putc(',', ftable);
  428.  
  429.         if (j >= 10)
  430.         {
  431.             putc('\n', ftable);
  432.             j = 1;
  433.         }
  434.         else
  435.         {
  436.             j++;
  437.         }
  438.  
  439.         fprintf(ftable, "%4d", rlhs[i]);
  440.     }
  441.  
  442.     FREE(rlhs + 1);
  443.  
  444.     fprintf(ftable, "\n};\n\nstatic const short yyr2[] = {     0");
  445.  
  446.     j = 10;
  447.     for (i = 1; i < nrules; i++)
  448.     {
  449.         putc(',', ftable);
  450.  
  451.         if (j >= 10)
  452.         {
  453.             putc('\n', ftable);
  454.             j = 1;
  455.         }
  456.         else
  457.         {
  458.             j++;
  459.         }
  460.  
  461.         fprintf(ftable, "%4d", rrhs[i + 1] - rrhs[i] - 1);
  462.     }
  463.  
  464.     putc(',', ftable);
  465.     if (j >= 10)
  466.         putc('\n', ftable);
  467.  
  468.     fprintf(ftable, "%4d\n};\n", nitems - rrhs[nrules] - 1);
  469.     FREE(rrhs + 1);
  470. }
  471.  
  472.  
  473.  
  474. void output_defines()
  475. {
  476.     fprintf(ftable, "\n\n#define\tYYFINAL\t\t%d\n", final_state);
  477.     fprintf(ftable, "#define\tYYFLAG\t\t%d\n", MINSHORT);
  478.     fprintf(ftable, "#define\tYYNTBASE\t%d\n", ntokens);
  479. }
  480.  
  481.  
  482.  
  483. /* compute and output yydefact, yydefgoto, yypact, yypgoto, yytable and yycheck.  */
  484.  
  485. void output_actions()
  486. {
  487.     nvectors = nstates + nvars;
  488.  
  489.     froms = NEW2(nvectors, short *);
  490.     tos = NEW2(nvectors, short *);
  491.     tally = NEW2(nvectors, short);
  492.     width = NEW2(nvectors, short);
  493.  
  494.     token_actions();
  495.     free_shifts();
  496.     free_reductions();
  497.     FREE(lookaheads);
  498.     FREE(LA);
  499.     FREE(LAruleno);
  500.     FREE(accessing_symbol);
  501.  
  502.     goto_actions();
  503.     FREE(goto_map + ntokens);
  504.     FREE(from_state);
  505.     FREE(to_state);
  506.  
  507.     sort_actions();
  508.     pack_table();
  509.     output_base();
  510.     output_table();
  511.     output_check();
  512. }
  513.  
  514.  
  515.  
  516. /* figure out the actions for the specified state, indexed by lookahead token type.
  517.  
  518.    The yydefact table is output now.  The detailed info
  519.    is saved for putting into yytable later.  */
  520.  
  521. void token_actions()
  522. {
  523.     register int i;
  524.     register int j;
  525.     register int k;
  526.  
  527.     actrow = NEW2(ntokens, short);
  528.  
  529.     k = action_row(0);
  530.     fprintf(ftable, "\nstatic const short yydefact[] = {%4d", k);
  531.     save_row(0);
  532.  
  533.     j = 10;
  534.     for (i = 1; i < nstates; i++)
  535.     {
  536.         putc(',', ftable);
  537.  
  538.         if (j >= 10)
  539.         {
  540.             putc('\n', ftable);
  541.             j = 1;
  542.         }
  543.         else
  544.         {
  545.             j++;
  546.         }
  547.  
  548.         k = action_row(i);
  549.         fprintf(ftable, "%4d", k);
  550.         save_row(i);
  551.     }
  552.  
  553.     fprintf(ftable, "\n};\n");
  554.     FREE(actrow);
  555. }
  556.  
  557.  
  558.  
  559. /* Decide what to do for each type of token if seen as the lookahead token in specified state.
  560.    The value returned is used as the default action (yydefact) for the state.
  561.    In addition, actrow is filled with what to do for each kind of token,
  562.    index by symbol number, with zero meaning do the default action.
  563.    The value MINSHORT, a very negative number, means this situation
  564.    is an error.  The parser recognizes this value specially.
  565.  
  566.    This is where conflicts are resolved.  The loop over lookahead rules
  567.    considered lower-numbered rules last, and the last rule considered that likes
  568.    a token gets to handle it.  */
  569.  
  570. int
  571. action_row(state)
  572. int state;
  573. {
  574.     register int i;
  575.     register int j;
  576.     register int k;
  577.     register int m;
  578.     register int n;
  579.     register int count;
  580.     register int default_rule;
  581.     register int nreds;
  582.     register int max;
  583.     register int rule;
  584.     register int shift_state;
  585.     register int symbol;
  586.     register unsigned mask;
  587.     register unsigned *wordp;
  588.     register reductions *redp;
  589.     register shifts *shiftp;
  590.     register errs *errp;
  591.     int nodefault = 0;  /* set nonzero to inhibit having any default reduction */
  592.  
  593.     for (i = 0; i < ntokens; i++)
  594.         actrow[i] = 0;
  595.  
  596.     default_rule = 0;
  597.     nreds = 0;
  598.     redp = reduction_table[state];
  599.  
  600.     if (redp)
  601.     {
  602.         nreds = redp->nreds;
  603.  
  604.  
  605.         if (nreds >= 1)
  606.         {
  607.             /* loop over all the rules available here which require lookahead */
  608.             m = lookaheads[state];
  609.             n = lookaheads[state + 1];
  610.  
  611.             for (i = n - 1; i >= m; i--)
  612.             {
  613.                 rule = - LAruleno[i];
  614.                 wordp = LA + i * tokensetsize;
  615.                 mask = 1;
  616.  
  617.                 /* and find each token which the rule finds acceptable to come next */
  618.                 for (j = 0; j < ntokens; j++)
  619.                 {
  620.                     /* and record this rule as the rule to use if that token follows.  */
  621.                     if (mask & *wordp)
  622.                         actrow[j] = rule;
  623.  
  624.                     mask <<= 1;
  625.                     if (mask == 0)
  626.                     {
  627.                         mask = 1;
  628.                         wordp++;
  629.                     }
  630.                 }
  631.             }
  632.         }
  633.     }
  634.  
  635.     shiftp = shift_table[state];
  636.  
  637.     /* now see which tokens are allowed for shifts in this state.
  638.      For them, record the shift as the thing to do.  So shift is preferred to reduce.  */
  639.  
  640.     if (shiftp)
  641.     {
  642.         k = shiftp->nshifts;
  643.  
  644.         for (i = 0; i < k; i++)
  645.         {
  646.             shift_state = shiftp->shifts[i];
  647.             if (! shift_state) continue;
  648.  
  649.             symbol = accessing_symbol[shift_state];
  650.  
  651.             if (ISVAR(symbol))
  652.                 break;
  653.  
  654.             actrow[symbol] = shift_state;
  655.  
  656.             /* do not use any default reduction if there is a shift for error */
  657.  
  658.             if (symbol == error_token_number) nodefault = 1;
  659.         }
  660.     }
  661.  
  662.     errp = err_table[state];
  663.  
  664.     /* See which tokens are an explicit error in this state
  665.      (due to %nonassoc).  For them, record MINSHORT as the action.  */
  666.  
  667.     if (errp)
  668.     {
  669.         k = errp->nerrs;
  670.  
  671.         for (i = 0; i < k; i++)
  672.         {
  673.             symbol = errp->errs[i];
  674.             actrow[symbol] = MINSHORT;
  675.         }
  676.     }
  677.  
  678.     /* now find the most common reduction and make it the default action for this state.  */
  679.  
  680.     if (nreds >= 1 && ! nodefault)
  681.     {
  682.         if (consistent[state])
  683.         default_rule = redp->rules[0];
  684.         else
  685.         {
  686.             max = 0;
  687.             for (i = m; i < n; i++)
  688.             {
  689.                 count = 0;
  690.                 rule = - LAruleno[i];
  691.  
  692.                 for (j = 0; j < ntokens; j++)
  693.                 {
  694.                     if (actrow[j] == rule)
  695.                         count++;
  696.                 }
  697.  
  698.                 if (count > max)
  699.                 {
  700.                     max = count;
  701.                     default_rule = rule;
  702.                 }
  703.             }
  704.  
  705.             /* actions which match the default are replaced with zero,
  706.          which means "use the default" */
  707.  
  708.             if (max > 0)
  709.             {
  710.                 for (j = 0; j < ntokens; j++)
  711.                 {
  712.                     if (actrow[j] == default_rule)
  713.                         actrow[j] = 0;
  714.                 }
  715.  
  716.             default_rule = - default_rule;
  717.             }
  718.         }
  719.     }
  720.  
  721.     /* If have no default rule, the default is an error.
  722.      So replace any action which says "error" with "use default".  */
  723.  
  724.     if (default_rule == 0)
  725.         for (j = 0; j < ntokens; j++)
  726.         {
  727.             if (actrow[j] == MINSHORT)
  728.                 actrow[j] = 0;
  729.         }
  730.  
  731.     return (default_rule);
  732. }
  733.  
  734.  
  735.  
  736. void save_row(state)
  737. int state;
  738. {
  739.     register int i;
  740.     register int count;
  741.     register short *sp;
  742.     register short *sp1;
  743.     register short *sp2;
  744.  
  745.     count = 0;
  746.     for (i = 0; i < ntokens; i++)
  747.     {
  748.         if (actrow[i] != 0)
  749.             count++;
  750.     }
  751.  
  752.     if (count == 0)
  753.         return;
  754.  
  755.     froms[state] = sp1 = sp = NEW2(count, short);
  756.     tos[state] = sp2 = NEW2(count, short);
  757.  
  758.     for (i = 0; i < ntokens; i++)
  759.     {
  760.         if (actrow[i] != 0)
  761.         {
  762.             *sp1++ = i;
  763.             *sp2++ = actrow[i];
  764.         }
  765.     }
  766.  
  767.     tally[state] = count;
  768.     width[state] = sp1[-1] - sp[0] + 1;
  769. }
  770.  
  771.  
  772.  
  773. /* figure out what to do after reducing with each rule,
  774.    depending on the saved state from before the beginning
  775.    of parsing the data that matched this rule.
  776.  
  777.    The yydefgoto table is output now.  The detailed info
  778.    is saved for putting into yytable later.  */
  779.  
  780. void goto_actions()
  781. {
  782.     register int i;
  783.     register int j;
  784.     register int k;
  785.  
  786.     state_count = NEW2(nstates, short);
  787.  
  788.     k = default_goto(ntokens);
  789.     fprintf(ftable, "\nstatic const short yydefgoto[] = {%6d", k);
  790.     save_column(ntokens, k);
  791.  
  792.     j = 10;
  793.     for (i = ntokens + 1; i < nsyms; i++)
  794.     {
  795.         putc(',', ftable);
  796.  
  797.         if (j >= 10)
  798.         {
  799.             putc('\n', ftable);
  800.             j = 1;
  801.         }
  802.         else
  803.         {
  804.             j++;
  805.         }
  806.  
  807.         k = default_goto(i);
  808.         fprintf(ftable, "%6d", k);
  809.         save_column(i, k);
  810.     }
  811.  
  812.     fprintf(ftable, "\n};\n");
  813.     FREE(state_count);
  814. }
  815.  
  816.  
  817.  
  818. int
  819. default_goto(symbol)
  820. int symbol;
  821. {
  822.     register int i;
  823.     register int m;
  824.     register int n;
  825.     register int default_state;
  826.     register int max;
  827.  
  828.     m = goto_map[symbol];
  829.     n = goto_map[symbol + 1];
  830.  
  831.     if (m == n)
  832.         return (-1);
  833.  
  834.     for (i = 0; i < nstates; i++)
  835.         state_count[i] = 0;
  836.  
  837.     for (i = m; i < n; i++)
  838.         state_count[to_state[i]]++;
  839.  
  840.     max = 0;
  841. default_state = -1;
  842.  
  843.     for (i = 0; i < nstates; i++)
  844.     {
  845.         if (state_count[i] > max)
  846.         {
  847.             max = state_count[i];
  848.         default_state = i;
  849.         }
  850.     }
  851.  
  852.     return (default_state);
  853. }
  854.  
  855.  
  856.  
  857. void save_column(symbol, default_state)
  858. int symbol;
  859. int default_state;
  860. {
  861.     register int i;
  862.     register int m;
  863.     register int n;
  864.     register short *sp;
  865.     register short *sp1;
  866.     register short *sp2;
  867.     register int count;
  868.     register int symno;
  869.  
  870.     m = goto_map[symbol];
  871.     n = goto_map[symbol + 1];
  872.  
  873.     count = 0;
  874.     for (i = m; i < n; i++)
  875.     {
  876.         if (to_state[i] != default_state)
  877.             count++;
  878.     }
  879.  
  880.     if (count == 0)
  881.         return;
  882.  
  883.     symno = symbol - ntokens + nstates;
  884.  
  885.     froms[symno] = sp1 = sp = NEW2(count, short);
  886.     tos[symno] = sp2 = NEW2(count, short);
  887.  
  888.     for (i = m; i < n; i++)
  889.     {
  890.         if (to_state[i] != default_state)
  891.         {
  892.             *sp1++ = from_state[i];
  893.             *sp2++ = to_state[i];
  894.         }
  895.     }
  896.  
  897.     tally[symno] = count;
  898.     width[symno] = sp1[-1] - sp[0] + 1;
  899. }
  900.  
  901.  
  902.  
  903. /* the next few functions decide how to pack 
  904.    the actions and gotos information into yytable. */
  905.  
  906. void sort_actions()
  907. {
  908.     register int i;
  909.     register int j;
  910.     register int k;
  911.     register int t;
  912.     register int w;
  913.  
  914.     order = NEW2(nvectors, short);
  915.     nentries = 0;
  916.  
  917.     for (i = 0; i < nvectors; i++)
  918.     {
  919.         if (tally[i] > 0)
  920.         {
  921.             t = tally[i];
  922.             w = width[i];
  923.             j = nentries - 1;
  924.  
  925.             while (j >= 0 && (width[order[j]] < w))
  926.                 j--;
  927.  
  928.             while (j >= 0 && (width[order[j]] == w) && (tally[order[j]] < t))
  929.                 j--;
  930.  
  931.             for (k = nentries - 1; k > j; k--)
  932.                 order[k + 1] = order[k];
  933.  
  934.             order[j + 1] = i;
  935.             nentries++;
  936.         }
  937.     }
  938. }
  939.  
  940.  
  941.  
  942. void pack_table()
  943. {
  944.     register int i;
  945.     register int place;
  946.     register int state;
  947.  
  948.     base = NEW2(nvectors, short);
  949.     pos = NEW2(nentries, short);
  950.     table = NEW2(MAXTABLE, short);
  951.     check = NEW2(MAXTABLE, short);
  952.  
  953.     lowzero = 0;
  954.     high = 0;
  955.  
  956.     for (i = 0; i < nvectors; i++)
  957.         base[i] = MINSHORT;
  958.  
  959.     for (i = 0; i < MAXTABLE; i++)
  960.         check[i] = -1;
  961.  
  962.     for (i = 0; i < nentries; i++)
  963.     {
  964.         state = matching_state(i);
  965.  
  966.         if (state < 0)
  967.             place = pack_vector(i);
  968.         else
  969.             place = base[state];
  970.  
  971.         pos[i] = place;
  972.         base[order[i]] = place;
  973.     }
  974.  
  975.     for (i = 0; i < nvectors; i++)
  976.     {
  977.         FREE(froms[i]);
  978.         FREE(tos[i]);
  979.     }
  980.  
  981.     FREE(froms);
  982.     FREE(tos);
  983.     FREE(pos);
  984. }
  985.  
  986.  
  987.  
  988. int
  989. matching_state(vector)
  990. int vector;
  991. {
  992.     register int i;
  993.     register int j;
  994.     register int k;
  995.     register int t;
  996.     register int w;
  997.     register int match;
  998.     register int prev;
  999.  
  1000.     i = order[vector];
  1001.     if (i >= nstates)
  1002.         return (-1);
  1003.  
  1004.     t = tally[i];
  1005.     w = width[i];
  1006.  
  1007.     for (prev = vector - 1; prev >= 0; prev--)
  1008.     {
  1009.         j = order[prev];
  1010.         if (width[j] != w || tally[j] != t)
  1011.             return (-1);
  1012.  
  1013.         match = 1;
  1014.         for (k = 0; match && k < t; k++)
  1015.         {
  1016.             if (tos[j][k] != tos[i][k] || froms[j][k] != froms[i][k])
  1017.                 match = 0;
  1018.         }
  1019.  
  1020.         if (match)
  1021.             return (j);
  1022.     }
  1023.  
  1024.     return (-1);
  1025. }
  1026.  
  1027.  
  1028.  
  1029. int
  1030. pack_vector(vector)
  1031. int vector;
  1032. {
  1033.     register int i;
  1034.     register int j;
  1035.     register int k;
  1036.     register int t;
  1037.     register int loc;
  1038.     register int ok;
  1039.     register short *from;
  1040.     register short *to;
  1041.  
  1042.     i = order[vector];
  1043.     t = tally[i];
  1044.  
  1045.     if (t == 0)
  1046.         berror("pack_vector");
  1047.  
  1048.     from = froms[i];
  1049.     to = tos[i];
  1050.  
  1051.     for (j = lowzero - from[0]; j < MAXTABLE; j++)
  1052.     {
  1053.         ok = 1;
  1054.  
  1055.         for (k = 0; ok && k < t; k++)
  1056.         {
  1057.             loc = j + from[k];
  1058.             if (loc > MAXTABLE)
  1059.                 fatals("maximum table size (%d) exceeded",MAXTABLE);
  1060.  
  1061.             if (table[loc] != 0)
  1062.                 ok = 0;
  1063.         }
  1064.  
  1065.         for (k = 0; ok && k < vector; k++)
  1066.         {
  1067.             if (pos[k] == j)
  1068.                 ok = 0;
  1069.         }
  1070.  
  1071.         if (ok)
  1072.         {
  1073.             for (k = 0; k < t; k++)
  1074.             {
  1075.                 loc = j + from[k];
  1076.                 table[loc] = to[k];
  1077.                 check[loc] = from[k];
  1078.             }
  1079.  
  1080.             while (table[lowzero] != 0)
  1081.                 lowzero++;
  1082.  
  1083.             if (loc > high)
  1084.                 high = loc;
  1085.  
  1086.             return (j);
  1087.         }
  1088.     }
  1089.  
  1090.     berror("pack_vector");
  1091.     return 0;    /* JF keep lint happy */
  1092. }
  1093.  
  1094.  
  1095.  
  1096. /* the following functions output yytable, yycheck
  1097.    and the vectors whose elements index the portion starts */
  1098.  
  1099. void output_base()
  1100. {
  1101.     register int i;
  1102.     register int j;
  1103.     fprintf(ftable, "\nstatic const short yypact[] = {%6d", base[0]);
  1104.  
  1105.     j = 10;
  1106.     for (i = 1; i < nstates; i++)
  1107.     {
  1108.         putc(',', ftable);
  1109.  
  1110.         if (j >= 10)
  1111.         {
  1112.             putc('\n', ftable);
  1113.             j = 1;
  1114.         }
  1115.         else
  1116.         {
  1117.             j++;
  1118.         }
  1119.  
  1120.         fprintf(ftable, "%6d", base[i]);
  1121.     }
  1122.  
  1123.     fprintf(ftable, "\n};\n\nstatic const short yypgoto[] = {%6d", base[nstates]);
  1124.  
  1125.     j = 10;
  1126.     for (i = nstates + 1; i < nvectors; i++)
  1127.     {
  1128.         putc(',', ftable);
  1129.  
  1130.         if (j >= 10)
  1131.         {
  1132.             putc('\n', ftable);
  1133.             j = 1;
  1134.         }
  1135.         else
  1136.         {
  1137.             j++;
  1138.         }
  1139.  
  1140.         fprintf(ftable, "%6d", base[i]);
  1141.     }
  1142.  
  1143.     fprintf(ftable, "\n};\n");
  1144.     FREE(base);
  1145. }
  1146.  
  1147.  
  1148.  
  1149. void output_table()
  1150. {
  1151.     register int i;
  1152.     register int j;
  1153.     fprintf(ftable, "\n\n#define\tYYLAST\t\t%d\n\n", high);
  1154.     fprintf(ftable, "\nstatic const short yytable[] = {%4d", table[0]);
  1155.  
  1156.     j = 10;
  1157.     for (i = 1; i <= high; i++)
  1158.     {
  1159.         putc(',', ftable);
  1160.  
  1161.         if (j >= 10)
  1162.         {
  1163.             putc('\n', ftable);
  1164.             j = 1;
  1165.         }
  1166.         else
  1167.         {
  1168.             j++;
  1169.         }
  1170.  
  1171.         fprintf(ftable, "%4d", table[i]);
  1172.     }
  1173.  
  1174.     fprintf(ftable, "\n};\n");
  1175.     FREE(table);
  1176. }
  1177.  
  1178.  
  1179.  
  1180. void output_check()
  1181. {
  1182.     register int i;
  1183.     register int j;
  1184.     fprintf(ftable, "\nstatic const short yycheck[] = {%4d", check[0]);
  1185.  
  1186.     j = 10;
  1187.     for (i = 1; i <= high; i++)
  1188.     {
  1189.         putc(',', ftable);
  1190.  
  1191.         if (j >= 10)
  1192.         {
  1193.             putc('\n', ftable);
  1194.             j = 1;
  1195.         }
  1196.         else
  1197.         {
  1198.             j++;
  1199.         }
  1200.  
  1201.         fprintf(ftable, "%4d", check[i]);
  1202.     }
  1203.  
  1204.     fprintf(ftable, "\n};\n");
  1205.     FREE(check);
  1206. }
  1207.  
  1208.  
  1209.  
  1210. /*
  1211.     DrH 1/6/89
  1212.         test for "#line" or "#    line" directive in parser file.
  1213. */
  1214.  
  1215. int has_linexp( register char *s)
  1216. {
  1217.  register char *t ;
  1218.                                             /* skip leading whitespace */
  1219.     while ((*s) && isspace( *s)) ++s ;
  1220.                                 
  1221.     if (t = strchr( s, '#'))
  1222.     {
  1223.         ++t ;                                 /* skip over '#' */
  1224.  
  1225.         while ((*t) && isspace( *t)) ++t ;    /* skip intervening whitespace */
  1226.  
  1227.         if ( *t != 'l' )                      /* quick exit */
  1228.             return FALSE ;
  1229.  
  1230.         return (( strncmp( t, "line", 4)) ? FALSE : TRUE ) ;
  1231.     }
  1232.     return FALSE ;
  1233. }
  1234.  
  1235.  
  1236.  
  1237.  
  1238. /* copy the parser code into the ftable file at the end.  */
  1239.  
  1240. void output_parser()
  1241. {
  1242.     register int c, bytesread;
  1243.     char linebuf[256], *sptr ;
  1244. #ifdef DONTDEF
  1245.     FILE *fpars;
  1246. #else
  1247. #define fpars fparser
  1248. #endif
  1249.  
  1250.     if (pure_parser)
  1251.         fprintf(ftable, "#define YYIMPURE 1\n\n");
  1252.     else
  1253.         fprintf(ftable, "#define YYPURE 1\n\n");
  1254.  
  1255. #ifdef DONTDEF    /* JF no longer needed 'cuz open_extra_files changes the
  1256.                     currently open parser from bison.simple to bison.hairy */
  1257.     if (semantic_parser)
  1258.         fpars = fparser;
  1259.     else fpars = fparser1;
  1260.  
  1261. #endif
  1262.  
  1263. /*
  1264. DrH 1/6/89
  1265.     Fixed '#line' detector so that it (A.) actually works, and
  1266.     (B.) doesn't insert an EOF in the middle of the output file,
  1267.     just like the old PD-YACC.
  1268.  
  1269.     NOTE: file activity is now line and block-oriented since the
  1270.             operation is largely file copying.
  1271. */
  1272.     while (sptr = fgets( &linebuf[0], 255, fpars))
  1273.     {
  1274.         if (nolinesflag)
  1275.         {
  1276.             if ( has_linexp( &linebuf[0]))
  1277.                 continue ;
  1278.         }
  1279.  
  1280.         if (sptr = strchr( &linebuf[0], '$'))
  1281.         {
  1282.             fputs( (sptr+1), ftable) ;              /* move past the '$' */
  1283.                                                     /* and write the rest */
  1284. #ifdef DONTDEF
  1285.             fprintf(ftable, "#include \"%s\"\n", actfile);
  1286. #else
  1287.             /* JF don't #include the action file.  Stuff it right in. */
  1288.             /* DrH  1/6/89  block file I/O */
  1289.  
  1290.             rewind(faction);
  1291.  
  1292.             sptr = NEW2(DOSTRACK, char) ;        /* 4608 = DOS bytes per track */
  1293.  
  1294.             while ((bytesread = fread( sptr, 1, DOSTRACK, faction)) > 0)
  1295.                 (void) fwrite( sptr, 1, bytesread, ftable) ;
  1296.  
  1297.             FREE( sptr) ;
  1298. #endif
  1299.         }
  1300.         else
  1301.             fputs( &linebuf[0], ftable) ;
  1302.  
  1303.     } /* -------------- end of while */
  1304. }
  1305.  
  1306.  
  1307.  
  1308. void output_program()
  1309. {
  1310.     register int c;
  1311.     char *buff ;
  1312.     int bytesread ;
  1313.  
  1314.     extern int lineno;
  1315.  
  1316.     buff = NEW2( DOSTRACK, char) ;
  1317.  
  1318.     if (!nolinesflag)
  1319.         fprintf(ftable, "#line %d \"%s\"\n", lineno, infile);
  1320.  
  1321. /* DrH 1/6/89 block file I/O */
  1322.  
  1323.     while ((bytesread = fread( buff, 1, DOSTRACK, finput)) > 0)
  1324.         (void) fwrite( buff, 1, bytesread, ftable) ;
  1325.  
  1326.     FREE( buff) ;
  1327. }
  1328.  
  1329.  
  1330.  
  1331. void free_itemsets()
  1332. {
  1333.     register core *cp,*cptmp;
  1334.  
  1335.     FREE(state_table);
  1336.  
  1337.     for (cp = first_state; cp; cp = cptmp) {
  1338.         cptmp=cp->next;
  1339.         FREE(cp);
  1340.     }
  1341. }
  1342.  
  1343.  
  1344.  
  1345. void free_shifts()
  1346. {
  1347.     register shifts *sp,*sptmp;/* JF derefrenced freed ptr */
  1348.  
  1349.     FREE(shift_table);
  1350.  
  1351.     for (sp = first_shift; sp; sp = sptmp) {
  1352.         sptmp=sp->next;
  1353.         FREE(sp);
  1354.     }
  1355. }
  1356.  
  1357.  
  1358.  
  1359. void free_reductions()
  1360. {
  1361.     register reductions *rp,*rptmp;/* JF fixed freed ptr */
  1362.  
  1363.     FREE(reduction_table);
  1364.  
  1365.     for (rp = first_reduction; rp; rp = rptmp) {
  1366.         rptmp=rp->next;
  1367.         FREE(rp);
  1368.     }
  1369. }
  1370.  
  1371.  
  1372.